package com.x.epspace.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;

import com.x.epspace.common.AppConfig;
import com.x.epspace.common.CodeUtil;
import com.x.epspace.common.JsonUtil;
import com.x.epspace.common.MessagesService;
import com.x.epspace.common.Tool;
import com.x.epspace.entity.BindEnt;
import com.x.epspace.ext.HandleSessionService;
import com.x.epspace.service.BindEntService;
import com.x.epspace.service.RegService;

@Controller("AajxAction")
public class AajxAction extends BaseAjaxController {
	
	private static final Log log = LogFactory.getLog(AajxAction.class);
	
	@Resource
	private RegService regService;
	@Resource
	private BindEntService bindEntService;
	@Resource
	private MessagesService messagesService;
	
	@Resource
	private HandleSessionService handleSession;
	
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String mobile = request.getParameter("mobile");
		String pwd = request.getParameter("pwd");
		String wxuserid = request.getParameter("wxuserid");
		Map<String,String> returnMap = regService.login(mobile, pwd ,wxuserid);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	/**
	 * 发送验证码
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void sendsmsv(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String mobile = request.getParameter("mobile");
		String checkcode = request.getParameter("checkcode");
		Map<String,String> returnMap = regService.sendValidSms(mobile,checkcode);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	/**
	 * 注册用户
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void reguser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String mobile = request.getParameter("mobile");
		String pwd = request.getParameter("pwd");
		String validcode = request.getParameter("validcode");
		String checkcode = request.getParameter("checkcode");
		String wxuserid = request.getParameter("wxuserid");
		Map<String,String> returnMap = regService.validateSmscode(mobile, validcode,checkcode);
		if(!returnMap.get("result").equals(AppConfig.getProperty("success", "return_code"))){
			this.outJsonString(response, JsonUtil.toJson(returnMap));
			return;
		}
		returnMap = regService.regUser(mobile, pwd,wxuserid);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	/**
	 * 验证并绑定企业信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void bindent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String szch = Tool.nvl(request.getParameter("szch"));
		String sqymc = Tool.nvl(request.getParameter("sqymc"));
		String identCode = Tool.nvl(request.getParameter("identCode"));
		String identName = Tool.nvl(request.getParameter("identName"));
		String identType = Tool.nvl(request.getParameter("identType"));
		String fzr = Tool.nvl(request.getParameter("fzr"));
		String fzrzjhm = Tool.nvl(request.getParameter("fzrzjhm"));
		Map<String, String> returnMap = bindEntService.validBindEnt(szch, sqymc, identCode, identName,identType,fzr,fzrzjhm);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	/**
	 * 重置密码
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void resetpwd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String mobile = request.getParameter("mobile");
		String newpwd = request.getParameter("newpwd");
		String validcode = request.getParameter("validcode");
		String checkcode = request.getParameter("checkcode");
		Map<String,String> returnMap = regService.validateSmscode(mobile, validcode,checkcode);
		if(!returnMap.get("result").equals(AppConfig.getProperty("success", "return_code"))){
			this.outJsonString(response, JsonUtil.toJson(returnMap));
			return;
		}
		returnMap = regService.resetPwd(mobile, newpwd);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}

	/**
	 * add chenwen
	 * 更新信息状态为已读
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void changemsgstate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String objId = request.getParameter("objId");
		messagesService.changeState(objId);
	}
	
	public void getmsgcount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.outJsonString(response,messagesService.getMsgCount());
	}
	
	/**
	 * 选择了绑定企业
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void selectbindLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String snbxh = request.getParameter("snbxh");
		Map<String, String> returnMap = bindEntService.selectBindEntLogin(snbxh);
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	/**
	 * 解绑当前登录企业身份
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void unBindent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, String> returnMap = bindEntService.unBindent();
		String returnJson = JsonUtil.toJson(returnMap);
		this.outJsonString(response,returnJson);
	}
	
	public void getCheckcode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("image/jpeg");
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires", 0);  
        CodeUtil.createCode(request.getSession(),response.getOutputStream(),"checkcode");
	}
	
}
